<template>
  <div class="news-list-container">
    <el-card>
      <template #header>
        <div class="header">
          <h2>新闻列表</h2>
          <el-select v-model="selectedCategory" placeholder="选择分类" @change="handleCategoryChange">
            <el-option label="全部" value="" />
            <el-option
              v-for="category in categories"
              :key="category.value"
              :label="category.label"
              :value="category.value"
            />
          </el-select>
        </div>
      </template>

      <el-table :data="newsList" style="width: 100%">
        <el-table-column prop="title" label="标题" min-width="200">
          <template #default="scope">
            <router-link :to="'/news/' + scope.row.id" class="news-title">
              {{ scope.row.title }}
            </router-link>
          </template>
        </el-table-column>
        <el-table-column prop="category" label="分类" width="120" />
        <el-table-column prop="createdAt" label="发布时间" width="180">
          <template #default="scope">
            {{ formatDate(scope.row.createdAt) }}
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useStore } from 'vuex'
import dayjs from 'dayjs'

export default {
  name: 'NewsList',
  setup() {
    const store = useStore()
    const newsList = ref([])
    const currentPage = ref(1)
    const pageSize = ref(10)
    const total = ref(0)
    const selectedCategory = ref('')

    const categories = [
      { label: '技术新闻', value: 'TECH' },
      { label: '活动报道', value: 'EVENT' },
      { label: '系统更新', value: 'SYSTEM' }
    ]

    const loadNews = async () => {
      try {
        const params = {
          page: currentPage.value - 1,
          size: pageSize.value,
          category: selectedCategory.value
        }
        
        const response = await store.dispatch('fetchNews', params)
        newsList.value = response.data.content
        total.value = response.data.total
      } catch (error) {
        console.error('Error loading news:', error)
      }
    }

    const handleSizeChange = (val) => {
      pageSize.value = val
      loadNews()
    }

    const handleCurrentChange = (val) => {
      currentPage.value = val
      loadNews()
    }

    const handleCategoryChange = () => {
      currentPage.value = 1
      loadNews()
    }

    const formatDate = (date) => {
      return dayjs(date).format('YYYY-MM-DD HH:mm')
    }

    onMounted(() => {
      loadNews()
    })

    return {
      newsList,
      currentPage,
      pageSize,
      total,
      categories,
      selectedCategory,
      handleSizeChange,
      handleCurrentChange,
      handleCategoryChange,
      formatDate
    }
  }
}
</script>

<style scoped>
.news-list-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

h2 {
  margin: 0;
}

.news-title {
  color: #409EFF;
  text-decoration: none;
}

.news-title:hover {
  text-decoration: underline;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>
